package com.hotel.controller;

import com.alibaba.fastjson.JSONObject;
import com.hotel.component.BaseController;
import com.hotel.component.IocContainer;
import com.hotel.entity.Option;
import com.hotel.entity.PageBean;
import com.hotel.entity.RoomType;
import com.hotel.exception.EntityException;
import com.hotel.service.RoomTypeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author Str
 * @Date 2021/11/1 22:08
 * @Description :
 */
@WebServlet("/roomType/*")
public class RoomTypeController extends BaseController {


    private RoomTypeService roomTypeService = (RoomTypeService) IocContainer.getBean("RoomTypeService");

    // 跳转到列表
    public String viewList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        return "forward:/jsp/roomType/list.jsp";
    }

    // 跳转到新增页
    public String viewAddType(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        return "forward:/jsp/roomType/addRoomType.jsp";
    }

    // 获取房间类型


    // 获取房间类型
    public String getTypeNameData(HttpServletRequest req, HttpServletResponse resp) {
        List<Option> options = new ArrayList();
        List<RoomType> roomTypes = null;
        JSONObject jsObj = null;
        try {
            jsObj = new JSONObject();
            roomTypes = roomTypeService.getRoomTypes();
            // 将楼层和id填充进Option列表
            for (RoomType roomType : roomTypes) {
                Option op = new Option(roomType.getTypeName(),roomType.getTypeName());
                options.add(op);
            }
            jsObj.put("result", "success");
            jsObj.put("options", options);
        } catch (SQLException e) {
            jsObj.put("result", "failed");
            e.printStackTrace();
        }
        return "data:" + jsObj;
    }

    public String getRoomType(HttpServletRequest req, HttpServletResponse resp) {
        //{1}获取 id 的值
        String id = req.getParameter("id");
        RoomType roomType = null;
        JSONObject jsObj = new JSONObject();
        String respTxt = "";
        try {
            //{2}根据 id 获取用户
            roomType = roomTypeService.getById(id);
            //{3}存入相关信息
            jsObj.put("result", "success");
            jsObj.put("roomType", roomType);
            respTxt = jsObj.toString();
        } catch (Exception e) {
            e.printStackTrace();
            respTxt = packErrMsg(e); //打包错误信息
        }
        return "data:" + respTxt;
    }

    // 获取房间类型名称、日价和id
    public String getRoomTypeData(HttpServletRequest req, HttpServletResponse resp) {
        List<Option> options = new ArrayList();
        List<RoomType> roomTypes = null;
        JSONObject jsObj = null;
        try {
            jsObj = new JSONObject();
            roomTypes = roomTypeService.getRoomTypes();
            // 将楼层和id填充进Option列表
            for (RoomType roomType : roomTypes) {
                Option op = new Option(roomType.getTypeName()+":"+roomType.getDayPrice(),roomType.getId());
                options.add(op);
            }
            jsObj.put("result", "success");
            jsObj.put("options", options);
        } catch (SQLException e) {
            jsObj.put("result", "failed");
            e.printStackTrace();
        }
        return "data:" + jsObj;
    }

    //任务(4)-新增 or 更新一个用户。
    //映射地址: /roomType/saveType
    public String saveType(HttpServletRequest req, HttpServletResponse resp)
            throws IOException, ServletException {
        RoomType roomType = makeRoomType(req);
        System.out.println("room============"+roomType);
        JSONObject jsObj = new JSONObject();
        String respTxt = "";
        try {
            roomTypeService.save(roomType);
            jsObj.put("result", "success");
            respTxt = jsObj.toString();
        } catch (Exception e) {
            e.printStackTrace();
            respTxt = packErrMsg(e); //打包错误信息
        }
        return "data:" + respTxt;
    }

    // 将返回的数据封装成对象
    public RoomType makeRoomType(HttpServletRequest req) {
        System.out.println("+----------------------------------------------------+");
        System.out.println( req.getParameter("typeName") );
        System.out.println("+----------------------------------------------------+");

        RoomType roomType = new RoomType();
        //{1}修改时使用。
        roomType.setId(req.getParameter("id"));
        //{2}新增时使用。
        roomType.setTypeName(req.getParameter("typeName"));
        roomType.setDayPrice( parseDouble(req.getParameter("dayPrice")) );
        roomType.setHourPrice( parseDouble(req.getParameter("hourPrice")) );
        return roomType;
    }


    public String delType(HttpServletRequest req, HttpServletResponse resp){
        JSONObject jo = new JSONObject();
        String id = req.getParameter("id");
        String respTxt = "";
        try {
            roomTypeService.delete(id);
            jo.put("result", "success");
            respTxt = jo.toString();
        } catch (SQLException | EntityException e) {
            e.printStackTrace();
            respTxt = packErrMsg(e); //打包错误信息
        }
        return "data:" + respTxt;
    }

    /**
     * 映射地址: /Notify/jsonList
     * 提交参数: page, limit, ...
     *
     * @return java.lang.String
     */
    public String jsonList(HttpServletRequest req, HttpServletResponse resp)
            throws IOException, ServletException {
        //{1}获取前端提交的关键字 [调BaseController的方法]
        Map<String, Object> keyWords = getKeyWords(req);
        for (String s : keyWords.keySet()) {
            System.out.println("key::" + s + ",value::" + keyWords.get(s));
        }
        String respTxt = "";
        PageBean<RoomType> page = null;
        try {
            //{2}调用 service 方法查询数据(传入关键字)
            page = roomTypeService.getList(keyWords);
            //{3}转成 json 格式数据.
            respTxt = page.toJSON();
        } catch (Exception e) {
            e.printStackTrace();  //包装错误信息..
            respTxt = packErrMsg(e);
        }
        //{4}返回数据到 LayUI 表格组件。
        return "data:" + respTxt;
    }

    private String packErrMsg(Exception e) {
        JSONObject jsObj = new JSONObject();
        jsObj.put("code", -1);
        jsObj.put("cause", e.getMessage());
        jsObj.put("result", "failed");
        //{ps}如果还有其它后面再加...
        return jsObj.toString();
    }
}
